<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/19
 * Time: 下午10:47
 */

namespace backend\controllers;


use Yii;
use yii\helpers\Url;
use yii\web\Controller;
use backend\components\MenuHelper;

class BaseController extends Controller
{
    const ADMIN_DOING = 'ADMIN_RUN';
    public $menu;
    public $menuHtml;

//    public function behaviors()
//    {
//
//        $behaviors = [
//            AdminLog::className(),//记录
//            Rbac::className(),//权限控制
//        ];
//        return array_merge( parent::behaviors(),$behaviors);
//    }


    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


    public function beforeAction($action)
    {
        parent::beforeAction($action);
        if (!$this->isLogin($action)) {
//          if (!MenuHelper::checkRoute ($this->route)) {
                //todo 没有权限处理
                return $this->redirect(['site/login']);
//          }
        }
        
//      exit('$action='.$action);
        
        if (!MenuHelper::checkRoute ($this->route)) {
//      	var_dump($this->route);
//      	echo 'wfuck='.$action;
			return $this->redirect(['site/login']);
        }
        return true;
    }

    /**
     * 验证登入
     * @return bool
     */
    protected function isLogin()
    {
        if (Yii::$app->user->isGuest) {
            $allowUrl = ['admin-user/changepass'];
            if (in_array($this->route, $allowUrl) == false) {
                $loginUrl = Url::toRoute(['site/login']);
                header("Location: $loginUrl");
                exit();
            } else {
                return false;
            }
        } else {
            return true;
        }

    }
}